package com.youlai.boot.gym.controller;

import com.youlai.boot.gym.service.EquipmentsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.gym.model.form.EquipmentsForm;
import com.youlai.boot.gym.model.query.EquipmentsQuery;
import com.youlai.boot.gym.model.vo.EquipmentsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 器材前端控制层
 *
 * @author hxc
 * @since 2025-06-01 10:04
 */
@Tag(name = "器材接口")
@RestController
@RequestMapping("/api/v1/equipments")
@RequiredArgsConstructor
public class EquipmentsController  {

    private final EquipmentsService equipmentsService;

    @Operation(summary = "器材分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('gym:equipments:query')")
    public PageResult<EquipmentsVO> getEquipmentsPage(EquipmentsQuery queryParams ) {
        IPage<EquipmentsVO> result = equipmentsService.getEquipmentsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增器材")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('gym:equipments:add')")
    public Result<Void> saveEquipments(@RequestBody @Valid EquipmentsForm formData ) {
        boolean result = equipmentsService.saveEquipments(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取器材表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('gym:equipments:edit')")
    public Result<EquipmentsForm> getEquipmentsForm(
        @Parameter(description = "器材ID") @PathVariable Long id
    ) {
        EquipmentsForm formData = equipmentsService.getEquipmentsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改器材")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('gym:equipments:edit')")
    public Result<Void> updateEquipments(
            @Parameter(description = "器材ID") @PathVariable Long id,
            @RequestBody @Validated EquipmentsForm formData
    ) {
        boolean result = equipmentsService.updateEquipments(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除器材")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('gym:equipments:delete')")
    public Result<Void> deleteEquipmentss(
        @Parameter(description = "器材ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = equipmentsService.deleteEquipmentss(ids);
        return Result.judge(result);
    }
}
